package com.baor.filter;

import com.alibaba.fastjson.JSONObject;
import com.baor.base.config.TokenService;
import com.baor.base.domain.UserVO;
import com.baor.base.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class GlobalFilterDefined implements GlobalFilter , Ordered {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("过滤器正在执行");
//        HttpHeaders httpHeaders =  exchange.getRequest().getHeaders();
//        httpHeaders.forEach((k,v)->{
//            if(k.equals("token")){
//
//            }
//        });
        ServerHttpRequest request = exchange.getRequest();
        if(request.getURI().toString().contains("login")){
            return chain.filter(exchange);
        }
        HttpHeaders headers = request.getHeaders();
        List<String> tokens = headers.getValuesAsList("token");
        JSONObject object = new JSONObject();
        object.put("code",403);
        object.put("msg","token 不存在或已过期");
        if(tokens.size() > 0){
            String token = tokens.get(0);
            UserVO userInfo = this.tokenService.getUserInfo(token);
            if(userInfo != null){
                Object value = this.redisUtil.getValue("loginUser:" + userInfo.getUserId());
                if(value != null && value.equals(token)){
                    return chain.filter(exchange);
                }else{
                    //TODO 执行错误不放过过滤器
                    return exchange.getResponse()
                            .writeWith(Flux.just(exchange.getResponse()
                                    .bufferFactory()
                                    .wrap(JSONObject.toJSONString(object).getBytes())));
                }
            }else{
                return exchange.getResponse()
                        .writeWith(Flux.just(exchange.getResponse()
                                .bufferFactory()
                                .wrap(JSONObject.toJSONString(object).getBytes())));
            }

        }else{
            return exchange.getResponse()
                    .writeWith(Flux.just(exchange.getResponse()
                            .bufferFactory()
                            .wrap(JSONObject.toJSONString(object).getBytes())));
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
